package com.terabits.smartriver.constants.result;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Yin on 2019/2/11
 * 错误码
 */
public enum ResultStatus {

    // -1为通用失败（根据ApiResult.java中的构造方法注释而来）
    FAIL(-1, "common fail"),
    UNKNOWN(9999, "未知错误。。。"),
    // 1为成功
    SUCCESS(1, "success"),

    error_pic_file(3, "非法图片文件"),
    error_pic_upload(4, "图片上传失败"),
    error_record_not_found(5, "没有找到对应的数据"),
    error_max_page_size(6, "请求记录数超出每次请求最大允许值"),
    error_create_failed(7, "新增失败"),
    error_update_failed(8, "修改失败"),
    error_delete_failed(9, "删除失败"),
    error_search_failed(10, "查询失败"),
    error_count_failed(11, "查询数据总数失败"),
    error_string_to_obj(12, "字符串转java对象失败"),
    error_invalid_argument(13, "参数不合法"),
    error_update_not_allowed(14, "更新失败：%s"),
    error_duplicated_data(15, "数据已存在"),
    error_unknown_database_operation(16, "未知数据库操作失败，请联系管理员解决"),
    error_column_unique(17, "字段s%违反唯一约束性条件"),
    error_file_download(18, "文件下载失败"),
    error_file_upload(19, "文件上传失败"),
    error_duplicated_imei(20, "imei重复"),

    //1000以内是系统错误，
    no_login(1000, "没有登录"),
    config_error(1001, "参数配置表错误"),
    user_exist(1002, "用户名已存在"),
    user_not_exist(1003, "用户名不存在！"),
    pwd_not_exist(1004, "密码错误！");

    private static final Logger LOGGER = LoggerFactory.getLogger(ResultStatus.class);

    private int status;
    private String msg;

    ResultStatus(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public static int getStatus(String define) {
        try {
            return ResultStatus.valueOf(define).status;
        } catch (IllegalArgumentException e) {
            LOGGER.error("undefined error status: {}", define);
            return FAIL.getErrorStatus();
        }
    }

    public static String getMsg(String define) {
        try {
            return ResultStatus.valueOf(define).msg;
        } catch (IllegalArgumentException e) {
            LOGGER.error("undefined status code: {}", define);
            return FAIL.getErrorMsg();
        }

    }

    public static String getMsg(int status) {
        for (ResultStatus err : ResultStatus.values()) {
            if (err.status == status) {
                return err.msg;
            }
        }
        return "errorStatus not defined ";
    }

    public int getErrorStatus() {
        return status;
    }

    public String getErrorMsg() {
        return msg;
    }

}

